package com.molin.edu.info.manager.service;

import com.molin.edu.info.manager.dao.BaseStudentDao;
import com.molin.edu.info.manager.dao.NewStudentDao;
import com.molin.edu.info.manager.dao.StudentDao;
import com.molin.edu.info.manager.domain.Student;
import com.molin.edu.info.manager.factory.StudentDaoFactory;

import java.util.ArrayList;
/*
* service不与用户交互，关心业务逻辑是否合理*/
public class StudentService {
//    StudentDao dao = new StudentDao();
//    NewStudentDao dao=new NewStudentDao();
//      紧耦合，使用Factory类

//    private BaseStudentDao dao=StudentDaoFactory.getStudentDao();
    private BaseStudentDao dao=StudentDaoFactory.getStudentDao("StudentDao");
    public boolean addStudent(String id, int age, String name, String birthday) {
        Student st = new Student(id, name, age, birthday);
        return dao.addStudent(st);

    }

    public boolean isExsit(String id) {
        ArrayList<Student> studentArrayList = new ArrayList<>();
        studentArrayList = findAll();
        boolean result = false;
        for (int i = 0; i < studentArrayList.size(); i++) {
            Student st = studentArrayList.get(i);
            if (st.getId().equals(id)) {
                result=true;
                break;
            }
        }
        return result;
    }

    public ArrayList<Student> findAll() {
        return dao.findAll();
    }

    public boolean dropStudentInfo(String id) {
        boolean result=false;
        if(isExsit(id)){
            result=dao.dropStudentInfo(fetchOne(id));
        }else {
            result=false;
        }
        return result;
    }

    public Student fetchOne(String id) {
        return dao.fetchOne(id);
    }

    public boolean modifyStudent(String id, int age, String name, String birthday) {
        Student st=new Student(id,name,age,birthday);
        //修改==删除&&添加
        return dao.modifyStudent(st);
    }
}
